/*
 * =====================================================================================
 *
 *       Filename:  log.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2014年06月16日 09时59分21秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef __LOG_H__
#define __LOG_H__
#include <syslog.h>
#include <pthread.h>
#include <string.h>
#include <arpa/inet.h>

extern int debug;
extern int daemon_enable;

#define __R_FILE__  ((strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__))

#define __sys_log(LEVEL, fmt, ...) 	\
	do { 				\
		if(LEVEL <= debug) { 	\
			if(daemon_enable) { 	\
				syslog(LEVEL|LOG_USER, "(%lu) %s +%d %s(): "fmt, 	\
					(unsigned long)pthread_self(), 			\
					__R_FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
			} else { 							\
				fprintf(stderr, "(%lu) %s +%d %s(): "fmt, 		\
					(unsigned long)pthread_self(),  		\
					__R_FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
			} 	\
		} 		\
	} while(0)

#define sys_err(fmt, ...) 	__sys_log(LOG_ERR, "ERR: "fmt, ##__VA_ARGS__)
#define sys_warn(fmt, ...) 	__sys_log(LOG_WARNING, "WARNING: "fmt, ##__VA_ARGS__)
#define sys_debug(fmt, ...) 	__sys_log(LOG_DEBUG, "DEBUG: "fmt, ##__VA_ARGS__)

#define __sys_log2(fmt, ...) 				\
	do { 						\
		fprintf(stderr, fmt,  ##__VA_ARGS__); 	\
	} while(0)
#define sys_info(fmt, ...) 	__sys_log2(fmt, ##__VA_ARGS__)

void pr_pkt(void *pkt, int len);
void pr_md5(void *md5);
void pr_ipv4(struct sockaddr_in *addr);
void pr_mac(void *mac);
#endif /* __LOG_H__ */
